package com.css.gdzj.gdzjbxgd.common.util.Restful;

import com.css.gdzj.gdzjbxgd.common.util.StaticAPI;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

public class RestfulUtil {

    public static Map sendRestful(HttpServletRequest request, Map<String, String> map, String url, HttpMethod method) {
        RestTemplate restTemplate = StaticAPI.getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        if (session.getAttribute("token") != null) {
            headers.set("token", session.getAttribute("token").toString());
        }
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<Map> result = restTemplate.exchange(url, method, requestEntity, Map.class);
        Map data = (Map) result.getBody();
        return data;
    }
}
